<template>
  <view class="container">
    <scroll-view scroll-y="true" style="box-sizing: border-box; width: 100%; height: 100vh" @scrolltolower="more">
      <view class="item" v-for="(item, index) in list" :key="item.id" @click="godatabankdetail(item)">
        <view>{{ item.title }}</view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      page: 1,
      list: [],
      format: ['doc', 'docx', 'pdf']
    };
  },
  methods: {
    // 获取我的资料
    async getmydata() {
      const res = await uni.$http.post('/news/downloadList', {
        page: this.page,
        type: '1'
      });
      if (res.data.data.list.length != 0) {
        this.list.push(...res.data.data.list);
      } else {
        uni.showToast({
          title: '数据已加载完毕',
          icon: 'none'
        });
      }
      console.log('资料库数据', res);
    },
    // 查看资料
    godatabankdetail(item) {
      console.log(item);
      if (this.format.includes(item.path.split('.')[1])) {
        // 直接打开资料
        uni.downloadFile({
          url: this.upimg + item.path,
          success: (res) => {
            if (res.statusCode === 200) {
              // console.log('下载成功');
              uni.openDocument({
                filePath: res.tempFilePath,
                success: function (res) {
                  console.log('打开文档成功');
                }
              });
            }
          }
        });
      } else {
        uni.showToast({
          title: '资料的格式不正确',
          icon: 'none'
        });
      }
    },
    // 加载更多
    more() {
      this.page++;
      this.getmydata();
    }
  },
  onLoad() {
    this.getmydata();
  }
};
</script>

<style lang="scss" scoped>
.container {
  box-sizing: border-box;
  .item {
    box-sizing: border-box;
    margin: 0 30rpx;
    padding: 40rpx 0;
    border-bottom: 2rpx solid #f1f1f1;
  }
}
</style>
